﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MINERP.BLL.CustomerManage;
using MINERP.ComControls;
using MINERP.MODEL.FoundationModel;
using FastReport;
using MINERP.ComControls.Extension;


namespace MINERP.CustomerManage
{
    public partial class FrmPrePrintBill : Form
    {
        public FrmPrePrintBill()
        {
            InitializeComponent();
            gvOrderInfo.ShowRowIndicator(50);
        }
        PrePrintBillBLL _bll = new PrePrintBillBLL();
        PrinterBill pbill = new PrinterBill();
        List<OrderProductUser> SelectList = new List<OrderProductUser>();
        bool isShowInfo = true;
        int CurrShopID = 0;
        int CurrBillidiID = 0;
        private void FrmPrePrintBill_Load(object sender, EventArgs e)
        {
            comboBoxDate.SelectedIndex = 0;
            comboBoxType.SelectedIndex = 0;
            gcOrderInfo.DataSource = GetOrderList();
            gcOrderInfo.RefreshDataSource();
        }

        private void simpleButtonPrePrice_Click(object sender, EventArgs e)
        {

        }
        List<OrderProductUser> GetOrderList()
        {
            List<OrderProductUser> rtnList = new List<OrderProductUser>();
            List<OrderProductUser> uNCountList = new List<OrderProductUser>();
            List<OrderProductUser> countList = new List<OrderProductUser>();
            DateTime startTime = DateTime.Now;
            DateTime endTime = DateTime.Now.AddDays(1).Date;
            int typeInt = 0;
            if (comboBoxDate.SelectedIndex == 0)
            {
                startTime = DateTime.Now.Date;
            }
            else if (comboBoxDate.SelectedIndex == 1)
            {
                startTime = DateTime.Now.AddDays(-1).Date;
            }
            else if (comboBoxDate.SelectedIndex == 2)
            {
                startTime = DateTime.Now.AddDays(-2).Date;
            }
            else if (comboBoxDate.SelectedIndex == 3)
            {
                startTime = DateTime.Now.AddDays(-7).Date;
            }
            else
            {
                startTime = DateTime.Now.AddDays(-30).Date;

            }
            if (comboBoxType.SelectedIndex == 0)
            {
                typeInt = 0;
                uNCountList = _bll.GetUnCountBill(startTime, endTime);
            }
            else if (comboBoxType.SelectedIndex == 1)
            {
                typeInt = 1;
                countList = _bll.GetCountBill(startTime, endTime);
            }
            else
            {

            }
            rtnList.AddRange(uNCountList);
            rtnList.AddRange(countList);

            return rtnList;
        }

        private void buttonOK_Click(object sender, EventArgs e)
        {
            backgroundWorkerGetData.RunWorkerAsync();
        }

        private void toolStripButtonRush_Click(object sender, EventArgs e)
        {
            gcOrderInfo.DataSource = GetOrderList();
            gcOrderInfo.RefreshDataSource();
        }

        private void toolStripButtonPrint_Click(object sender, EventArgs e)
        {
            if (SelectList.Count > 0)
            {
                List<int> Oklist = _bll.CheckOrder(SelectList.Select(s => s.OrderProductId).ToList());
                SHOP_Bill_Info sbi = _bll.OrganizationBillDataFromOrderList(Oklist, SelectList.FirstOrDefault().ShopID);
                if (sbi.Result == -1)
                {
                    MessageBox.Show("没有可打单的产品!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;

                }
                if (sbi.Result == -2)
                {
                    MessageBox.Show("已经打过结账单!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
                DataSet set = pbill.PrintBill(sbi);
                set.WriteXml(Application.StartupPath + @"\PrintShopBill.xml", XmlWriteMode.WriteSchema);
                Report report = new Report();
                report.Load(Application.StartupPath + "\\Report\\PrintShopBill.frx");
                report.RegisterData(set.Tables["tableTitle"], "tableTitle");
                report.RegisterData(set.Tables["tableDetail"], "tableDetail");
                report.RegisterData(set.Tables["tableSubDetail"], "tableSubDetail");
                //report.RegisterData(tableBottom, "tableBottom");
                //设置合计参数

                report.SetParameterValue("Para_TotalNum", set.Tables["tableTitle"].Rows[0]["Para_TotalNum"]);
                report.SetParameterValue("Para_TotalAreas", set.Tables["tableTitle"].Rows[0]["Para_TotalAreas"]);
                report.SetParameterValue("Para_TotalCount", set.Tables["tableTitle"].Rows[0]["Para_TotalCount"]);
                report.SetParameterValue("Para_Sum", set.Tables["tableTitle"].Rows[0]["Para_Sum"]);
                report.SetParameterValue("Para_TotalPrintCount", set.Tables["tableTitle"].Rows[0]["Para_TotalPrintCount"]);
                report.SetParameterValue("Para_TotalProductCount", set.Tables["tableTitle"].Rows[0]["Para_TotalProductCount"]);
                report.PrintSettings.ShowDialog = false;
                report.Print();


                List<OrderProductUser> listSrc = (List<OrderProductUser>)gvOrderInfo.DataSource;

                foreach (int opu in Oklist)
                {
                    listSrc.Remove(listSrc.FirstOrDefault(o => o.OrderProductId == opu));
                }
                gvOrderInfo.RefreshData();
                SelectList = new List<OrderProductUser>();
            }
            else
            {
                MessageBox.Show("未选择要打账单的产品", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }
        }

        private void gvOrderInfo_RowCellClick(object sender, DevExpress.XtraGrid.Views.Grid.RowCellClickEventArgs e)
        {
            if (e.RowHandle >= 0)
            {
                this.toolStripButton1ReportPrint.Enabled = false;
                if (e.Column.Name == "gridColumnSelect")
                {
                    OrderProductUser order = (OrderProductUser)gvOrderInfo.GetRow(e.RowHandle);
                    if (order.BillID == 0)
                    {
                        if (!order.IsSelect)
                        {
                            if (SelectList.Count == 0)
                            {

                                if (!order.IsCount)
                                {
                                    order.IsSelect = true;
                                    SelectList.Add(order);
                                }
                                else
                                {
                                    MessageBox.Show("该产品已经打印过账单不能重复打印", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                                }

                            }
                            else
                            {
                                if (SelectList.FirstOrDefault().ShopID == order.ShopID)
                                {
                                    order.IsSelect = true;
                                    SelectList.Add(order);
                                }
                                else
                                {

                                    MessageBox.Show("不同商户产品不能一起打印", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

                                }
                            }
                        }
                        else
                        {
                            order.IsSelect = false;
                        }
                    }
                    else
                    {
                        OrderProductUser orderC = (OrderProductUser)gvOrderInfo.GetRow(e.RowHandle);
                        this.textBoxPicPath.Text = orderC.OrderCode;
                        CurrShopID = orderC.ShopID;
                    }
                    gvOrderInfo.RefreshData();
                }
                else
                {
                    OrderProductUser orderC = (OrderProductUser)gvOrderInfo.GetRow(e.RowHandle);
                    //this.textBoxPicPath.Text = orderC.OrderCode;
                    if (orderC.BillID != 0)
                    {
                        CurrBillidiID = orderC.BillID;
                        this.toolStripButton1ReportPrint.Enabled = true;
                    }
                    else
                    {
                       
                        this.textBoxPicPath.Text = orderC.OrderCode;
                        CurrShopID = orderC.ShopID;
                    }
                }
            }
        }

        private void toolStripButtonSubPrint_Click(object sender, EventArgs e)
        {
            if (SelectList.Count > 0)
            {
                List<int> Oklist =_bll.CheckOrder(SelectList.Select(s => s.OrderProductId).ToList());

                List<SHOP_Bill_Info> sbiList = _bll.OrganizationBillDataListFromOrderList(Oklist, SelectList.FirstOrDefault().ShopID);
                foreach (SHOP_Bill_Info sbi in sbiList)
                {
                    DataSet set = pbill.PrintBill(sbi);
                    set.WriteXml(Application.StartupPath + @"\PrintShopBill.xml", XmlWriteMode.WriteSchema);
                    Report report = new Report();
                    report.Load(Application.StartupPath + "\\Report\\PrintShopBill.frx");
                    report.RegisterData(set.Tables["tableTitle"], "tableTitle");
                    report.RegisterData(set.Tables["tableDetail"], "tableDetail");
                    report.RegisterData(set.Tables["tableSubDetail"], "tableSubDetail");
                    //report.RegisterData(tableBottom, "tableBottom");
                    //设置合计参数

                    report.SetParameterValue("Para_TotalNum", set.Tables["tableTitle"].Rows[0]["Para_TotalNum"]);
                    report.SetParameterValue("Para_TotalAreas", set.Tables["tableTitle"].Rows[0]["Para_TotalAreas"]);
                    report.SetParameterValue("Para_TotalCount", set.Tables["tableTitle"].Rows[0]["Para_TotalCount"]);
                    report.SetParameterValue("Para_Sum", set.Tables["tableTitle"].Rows[0]["Para_Sum"]);
                    report.SetParameterValue("Para_TotalPrintCount", set.Tables["tableTitle"].Rows[0]["Para_TotalPrintCount"]);
                    report.SetParameterValue("Para_TotalProductCount", set.Tables["tableTitle"].Rows[0]["Para_TotalProductCount"]);
                    //report.Show();
                    report.PrintSettings.ShowDialog = false;
                    report.Print();
                    // Thread.Sleep(30000);
                }
                List<OrderProductUser> listSrc = (List<OrderProductUser>)gvOrderInfo.DataSource;

                foreach (int opu in Oklist)
                {
                    listSrc.Remove(listSrc.FirstOrDefault(o => o.OrderProductId == opu));
                }
                gvOrderInfo.RefreshData();
                SelectList = new List<OrderProductUser>();
            }
            else
            {
                MessageBox.Show("未选择要打账单的产品", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }
        }

        private void backgroundWorkerGetData_DoWork(object sender, DoWorkEventArgs e)
        {
            backgroundWorkerGetData.ReportProgress(0, "");
            List<OrderProductUser> list = GetOrderList();
            backgroundWorkerGetData.ReportProgress(1, list);
        }

        private void backgroundWorkerGetData_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            if (e.ProgressPercentage == 0)
            {
                this.Cursor = Cursors.WaitCursor;

            }
            else
            {
                gcOrderInfo.DataSource = (List<OrderProductUser>)e.UserState;
                gcOrderInfo.RefreshDataSource();
                this.Cursor = Cursors.Default;
            }
        }

        private void backgroundWorkerGetData_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {

        }

        private void buttonPrint_Click(object sender, EventArgs e)
        {
            string orderBagid = textBoxPicPath.Text;
            if (string.IsNullOrEmpty(orderBagid))
            {
                MessageBox.Show("未选择订单", "确定", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            List<OrderProductUser> listSrc = (List<OrderProductUser>)gvOrderInfo.DataSource;
            List<OrderProductUser> selectorder = listSrc.Where(p => p.OrderCode == orderBagid).ToList();
            //ist<int> Oklist = _bll.CheckOrder(SelectList.Select(s => s.OrderProductId).ToList());

            List<int> selectOid = _bll.CheckOrder(selectorder.Select(s => s.OrderProductId).ToList());
            DialogResult dr = MessageBox.Show("是否打印订单号为" + orderBagid + "的订单，分打账单请按‘是’，合打账单请按‘否’，不打印请按取消", "", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Information);
            if (dr == DialogResult.Yes)
            {
                List<SHOP_Bill_Info> sbiList = _bll.OrganizationBillDataListFromOrderList(selectOid, CurrShopID);
                foreach (SHOP_Bill_Info sbi in sbiList)
                {
                    DataSet set = pbill.PrintBill(sbi);
                    set.WriteXml(Application.StartupPath + @"\PrintShopBill.xml", XmlWriteMode.WriteSchema);
                    Report report = new Report();
                    report.Load(Application.StartupPath + "\\Report\\PrintShopBill.frx");
                    report.RegisterData(set.Tables["tableTitle"], "tableTitle");
                    report.RegisterData(set.Tables["tableDetail"], "tableDetail");
                    report.RegisterData(set.Tables["tableSubDetail"], "tableSubDetail");
                    //report.RegisterData(tableBottom, "tableBottom");
                    //设置合计参数

                    report.SetParameterValue("Para_TotalNum", set.Tables["tableTitle"].Rows[0]["Para_TotalNum"]);
                    report.SetParameterValue("Para_TotalAreas", set.Tables["tableTitle"].Rows[0]["Para_TotalAreas"]);
                    report.SetParameterValue("Para_TotalCount", set.Tables["tableTitle"].Rows[0]["Para_TotalCount"]);
                    report.SetParameterValue("Para_Sum", set.Tables["tableTitle"].Rows[0]["Para_Sum"]);
                    report.SetParameterValue("Para_TotalPrintCount", set.Tables["tableTitle"].Rows[0]["Para_TotalPrintCount"]);
                    report.SetParameterValue("Para_TotalProductCount", set.Tables["tableTitle"].Rows[0]["Para_TotalProductCount"]);
                    //report.Show();
                    report.PrintSettings.ShowDialog = false;
                    report.Print();
                    // Thread.Sleep(30000);
                }

                List<OrderProductUser> listSrcgV = (List<OrderProductUser>)gvOrderInfo.DataSource;

                foreach (int opuod in selectOid)
                {
                    listSrc.Remove(listSrc.FirstOrDefault(o => o.OrderProductId == opuod));
                }
                gvOrderInfo.RefreshData();
                //SelectList = new List<OrderProductUser>();

            }
            else if (dr == DialogResult.No)
            {
                SHOP_Bill_Info sbi = _bll.OrganizationBillDataFromOrderList(selectOid, CurrShopID);
                if (sbi.Result == -1)
                {
                    MessageBox.Show("没有可打单的产品!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;

                }
                if (sbi.Result == -2)
                {
                    MessageBox.Show("已经打过结账单!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
                DataSet set = pbill.PrintBill(sbi);
                set.WriteXml(Application.StartupPath + @"\PrintShopBill.xml", XmlWriteMode.WriteSchema);
                Report report = new Report();
                report.Load(Application.StartupPath + "\\Report\\PrintShopBill.frx");
                report.RegisterData(set.Tables["tableTitle"], "tableTitle");
                report.RegisterData(set.Tables["tableDetail"], "tableDetail");
                report.RegisterData(set.Tables["tableSubDetail"], "tableSubDetail");
                //report.RegisterData(tableBottom, "tableBottom");
                //设置合计参数

                report.SetParameterValue("Para_TotalNum", set.Tables["tableTitle"].Rows[0]["Para_TotalNum"]);
                report.SetParameterValue("Para_TotalAreas", set.Tables["tableTitle"].Rows[0]["Para_TotalAreas"]);
                report.SetParameterValue("Para_TotalCount", set.Tables["tableTitle"].Rows[0]["Para_TotalCount"]);
                report.SetParameterValue("Para_Sum", set.Tables["tableTitle"].Rows[0]["Para_Sum"]);
                report.SetParameterValue("Para_TotalPrintCount", set.Tables["tableTitle"].Rows[0]["Para_TotalPrintCount"]);
                report.SetParameterValue("Para_TotalProductCount", set.Tables["tableTitle"].Rows[0]["Para_TotalProductCount"]);
                report.PrintSettings.ShowDialog = false;
                report.Print();
                List<OrderProductUser> listSrcgV = (List<OrderProductUser>)gvOrderInfo.DataSource;

                foreach (int opuod in selectOid)
                {
                    listSrc.Remove(listSrc.FirstOrDefault(o => o.OrderProductId == opuod));
                }
                gvOrderInfo.RefreshData();
            }
            else
            {

            }

        }

        private void toolStripButton1ReportPrint_Click(object sender, EventArgs e)
        {
            try
            {
                this.toolStripButton1ReportPrint.Enabled = false;
                if (CurrBillidiID == 0)
                {
                    MessageBox.Show("未选择打印账单!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

                }
                if (DialogResult.OK == MessageBox.Show("是否重新打印账单号为" + CurrBillidiID, "确定", MessageBoxButtons.OK, MessageBoxIcon.Information))
                {
                    SHOP_Bill_Info sbi = _bll.GetBillByid(CurrBillidiID);
                    if (sbi.Result == -1)
                    {
                        MessageBox.Show("没有可打单的产品!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return;

                    }
                    if (sbi.Result == -2)
                    {
                        MessageBox.Show("已经打过结账单!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return;
                    }
                    DataSet set = pbill.PrintBill(sbi);
                    set.WriteXml(Application.StartupPath + @"\PrintShopBill.xml", XmlWriteMode.WriteSchema);
                    Report report = new Report();
                    report.Load(Application.StartupPath + "\\Report\\PrintShopBill.frx");
                    report.RegisterData(set.Tables["tableTitle"], "tableTitle");
                    report.RegisterData(set.Tables["tableDetail"], "tableDetail");
                    report.RegisterData(set.Tables["tableSubDetail"], "tableSubDetail");
                    //report.RegisterData(tableBottom, "tableBottom");
                    //设置合计参数

                    report.SetParameterValue("Para_TotalNum", set.Tables["tableTitle"].Rows[0]["Para_TotalNum"]);
                    report.SetParameterValue("Para_TotalAreas", set.Tables["tableTitle"].Rows[0]["Para_TotalAreas"]);
                    report.SetParameterValue("Para_TotalCount", set.Tables["tableTitle"].Rows[0]["Para_TotalCount"]);
                    report.SetParameterValue("Para_Sum", set.Tables["tableTitle"].Rows[0]["Para_Sum"]);
                    report.SetParameterValue("Para_TotalPrintCount", set.Tables["tableTitle"].Rows[0]["Para_TotalPrintCount"]);
                    report.SetParameterValue("Para_TotalProductCount", set.Tables["tableTitle"].Rows[0]["Para_TotalProductCount"]);
                    report.PrintSettings.ShowDialog = false;
                    report.Print();
                }
            }
            catch (Exception eeee)
            { }
            finally {
                if (CurrBillidiID != 0)
                {

                    this.toolStripButton1ReportPrint.Enabled = true;
                }
            }
           
        }

        private void tabPage1_Click(object sender, EventArgs e)
        {

        }
    }
}
